import { getStorage, setStorage } from '@/utils/auth'
import { userLogin } from '@/api/user'
import { getwechatopenidinfos } from '@/api/authorization'
//#ifdef H5
const code = window.localStorage.getItem('pro_code') || '';
//#endif

//#ifdef MP-WEIXIN
const code = getStorage("code") || '';
//#endif
const state = {
    hasLogin: code ? true : false,  //是否登陆
    code: code, //code
    addressInfo: getStorage('address_key'),
    openid: getStorage('openid_key'),
    indexSub: getStorage('indexSub_key'), //首页选中的课程
    questionsSub: getStorage('questionsSub_key'), //题库选中的课程
    courses: getStorage('courses_key'), // 用户课程
    subject: getStorage('subject_key'), // 用户专业
    userInfo: getStorage('userInfo_key'), // 用户信息
    orgInfo: getStorage('orgInfo_key'), // 机构信息
    headerName: getStorage('headerName_key'), //标题
    skinTheme: getStorage('skinTheme_Key'), //主题
    manager_id: getStorage('managerId_Key'), //商戶id
}
const mutations = {
    SET_CODE(state, code) {
        state.code = code
        setStorage('code_key', code)
    },
    SET_USER_CITY(state, address) {
        state.addressInfo = address
        setStorage('address_key', address)
    },
    SET_OPENID(state, open_id) {
        state.openid = open_id
        setStorage('openid_key', open_id)
    },
    SET_USER_COURSES(state, param) {
        state.courses = param
        setStorage('courses_key', state.courses)
    },
    SET_USER_SUBJECT(state, param) { //专业
        state.subject = param
        setStorage('subject_key', state.subject)
    },
    SELECT_FOR_INDEX(state, index) {
        state.indexSub = index
        setStorage('indexSub_key', index)
    },
    SELECT_FOR_QUESTIONS(state, index) {
        state.questionsSub = index
        setStorage('questionsSub_key', index)
    },
    SET_USER_INFO(state, index) {
        state.userInfo = index
        setStorage('userInfo_key', index)
    },
    SET_ORG_INFO(state, index) {
        state.orgInfo = index
        setStorage('orgInfo_key', index)
    },
    SET_HEADER_NAME(state, index) {
        state.headerName = index
        setStorage('headerName_key', index)
    },
    SET_SKIN_THEME(state, index) {
        state.skinTheme = index
        setStorage('skinTheme_Key', index)
    },
    SET_MANAGER_ID(state, index) {
        state.manager_id = index
        setStorage('managerId_Key', index)
    },
}
const actions = {
    setCode({ commit }, param) {
        commit('SET_CODE', param)
    },
    setUserAddress({ commit }, param) {
        commit('SET_USER_CITY', param)
    },
    setOpenid({ commit }, param) {
        commit('SET_OPENID', param)
    },
    setUserInfo({ commit }, param) { //用户信息
        commit('SET_USER_INFO', param)
    },
    setOrgInfo({ commit }, param) { //机构信息
        commit('SET_ORG_INFO', param)
    },
    setCourses({ commit }, param) { //课程
        commit('SET_USER_COURSES', param)
        // 初始话选中下标
        commit('SELECT_FOR_INDEX', 0)
        commit('SELECT_FOR_QUESTIONS', 0)
    },
    setSubject({ commit }, param) { //专业
        commit('SET_USER_SUBJECT', param)
    },
    selectForIndex({ commit }, param) { //选中课程
        commit('SELECT_FOR_INDEX', param)
    },
    selectForQuestion({ commit }, param) {
        commit('SELECT_FOR_QUESTIONS', param.index)
    },
    setHeaderName({ commit }, param) { //标题
        commit('SET_HEADER_NAME', param)
    },
    setSkinTheme({ commit }, param) { //主题
        commit('SET_SKIN_THEME', param)
    },
    setManagerId({ commit }, param) { //商戶id
        commit('SET_MANAGER_ID', param)
    },
    //登陆
    login(state) {
        handleLogin(state);
    },
}
function handleLogin(state) {
    //#ifdef H5
    // h5登陆 -> 发送url给后台，后端把code 写在本地localStorage中
    console.log('login', state)
    /*const URI = encodeURIComponent(window.location.href);
    console.log(123123,URI)
    const share_key = window.localStorage.getItem('share_key')
    const os = window.localStorage.getItem('os')
    window.location.href = `${window.appConfig.apiUrl}User.login/login?os=${os}&re_uri=${URI}&from_type=1&login_type=8&share_key=axx&client=1`;*/
    state.hasLogin = true
    // handleAddcar()
    //#endif
    //#ifdef MP-WEIXIN
    uni.login({
        success(loginRes) {
            console.log('code:',loginRes)
            const { code } = loginRes
            const login_type = 8
            // userLogin({ code, login_type }).then(({data}) => {
            // 	const { code } = data;
            // 	state.code = code
            //     state.hasLogin = true
            //     uni.navigateTo({
            //         url: `/pages/public/tologin`
            //     });

            // })
            uni.request({
                url: 'http://edubak.jstxm.cn/wechat/User.login/login', //仅为示例，并非真实接口地址。
                data: {
                    login_type: login_type,
                    code: code,
                    share_key: '',
                    from_type:1
                },
                dataType: 'json',
                header: { 'content-type': 'application/x-www-form-urlencoded' },
                success: (res) => {
                    //alert('授权')
                    console.log('res', res.data);
                    const code = res.data.data.code;
                    state.state.code = code
                    setStorage('code', code)
                    state.hasLogin = true
                    uni.navigateTo({
                        url: `/pages/public/tologin`
                    });
                    //this.text = 'request success';
                }
            });
        },
        fail(e) {
            console.log('errer->', e)
        }
    });
    //#endif
}
export default {
    namespaced: true,
    state,
    mutations,
    actions
}

